package org.tony.sim.fmt;

public class FmtJ extends Fmt32 {
    // J类型的指令IMM实际使用时要在最末尾拼接一个1'b0
    private static final byte[] IMM_BITS = new byte[]
            {31, 19, 18, 17, 16, 15, 14, 13, 12, 20, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21};

    protected FmtJ(Module module, Category category, String name, String description, int inst) {
        super(module, category, name, description, inst);
    }

    public byte getRd() {
        return (byte) getBits(super.inst, RD_BITS);
    }

    public void setRd(int rd) {
        super.inst = setBits(super.inst, rd, RD_BITS);
    }

    public short getImm() {
        return (short) getBits(super.inst, IMM_BITS);
    }

    public void setImm(int imm) {
        super.inst = setBits(super.inst, imm, IMM_BITS);
    }

    @Override
    public String toString() {
        String ret = super.toString();
        StringBuilder stringBuilder = new StringBuilder(ret.substring(0, ret.length() - 1));
        stringBuilder.append(", \"opcode\":\"").append(transToBinaryString(getOpcode(), OPCODE_BITS.length))
                .append("\", \"rd\":\"").append(transToBinaryString(getRd(), RD_BITS.length))
                .append("\", \"imm\":\"").append(transToBinaryString(getImm(), IMM_BITS.length))
                .append("\"}");
        return stringBuilder.toString();
    }
}
